package com.dingding.special.order.aboard.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.push.base.ClientAppType;
import com.dingding.special.order.aboard.code.ConfirmAboardCodeMsg;
import com.dingding.special.order.aboard.dao.IConfirmAboardDao;
import com.dingding.special.order.aboard.dto.ConfirmAboardNoticeDTO;
import com.dingding.special.order.aboard.dto.ReqConfirmAboardDTO;
import com.dingding.special.order.aboard.dto.ResConfirmAboardDTO;
import com.dingding.special.order.aboard.sql.ConfirmAboardSQL;

/**
 * 
 * @ClassName: ConfirmAboardDaoImpl 
 * @Description: 确认上车DAO实现类 
 * @author YHQ
 * @date 2016年8月21日 下午2:39:08
 * @since V2.4.0
 * 
 */
@SuppressWarnings("deprecation")
@Component("confirmAboardDao")
public class ConfirmAboardDaoImpl extends BaseDaoImpl<UserspecialorderT> implements IConfirmAboardDao{

	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 
	 * @Title: getSpecialOrdert 
	 * @Description: 根据用户Id获取专车未完成订单列表 
	 * @param userId 用户Id
	 * @author YHQ 
	 * @return List<Userspecialordert>    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public List<UserspecialorderT> getSpecialOrdert(String userId) {
		
		Criterion criterion = Restrictions.eq("grabOrderUserId", userId);
		
		try {
			return super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("根据用户Id获取专车未完成订单列表时出现异常   类："+this.getClass()+" getSpecialOrdert()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getSpecialOrdertInfo 
	 * @Description: 根据订单Id获取订单信息
	 * @param oid 订单Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return Userspecialordert    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public UserspecialorderT getSpecialOrdertInfo(String oid) {
		
		try {
			return super.getById(oid);
		} catch (Exception e) {
			log.error("根据订单Id获取订单信息时出现异常  类："+this.getClass()+" getSpecialOrdertInfo()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getNoticeInfo 
	 * @Description: 根据订单Id获取通知的信息 
	 * @param oid 订单Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return ConfirmAboardNoticeDTO    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public ConfirmAboardNoticeDTO getNoticeInfo(String oid) {
		
		try {
			
			// 根据订单Id获取订单信息
			@SuppressWarnings("unchecked")
			List<ConfirmAboardNoticeDTO> confirmAboardNoticeDTOs = (List<ConfirmAboardNoticeDTO>) super.listSqlFreeObject(ConfirmAboardSQL.GET_CONFIRM_ABOARD_NOTICE_INFO, ConfirmAboardNoticeDTO.class, oid);
		
			// 判断获取的通知消息是否存在
			if(confirmAboardNoticeDTOs != null && confirmAboardNoticeDTOs.size() > 0){
				
				ConfirmAboardNoticeDTO dto = confirmAboardNoticeDTOs.get(0);
				if(dto.getLoginEquipment() == 1){
					dto.setClientType(ClientAppType.iOS.toString());
				}else{
					dto.setClientType(ClientAppType.Android.toString());
				}
				
				return confirmAboardNoticeDTOs.get(0);
			}
			
		} catch (Exception e) {
			log.error("根据订单Id获取通知的信息 时出现异常  类："+this.getClass()+" getNoticeInfo()", e);
		}
		
		return null;
	}
	
	/**
	 * 
	 * @Title: confirmAboardOperation
	 * @Description: 确认上车操作
	 * @param response
	 *            确认上车响应Bean
	 * @param reqConfirmAboardDTO
	 *            确认上车客户端请求Bean
	 * @author YHQ
	 * @return void 返回类型
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
	public boolean confirmAboardOperation(ResponseEntity<ResConfirmAboardDTO> response,
			ReqConfirmAboardDTO reqConfirmAboardDTO) {

		// 根据订单Id获取订单信息
		UserspecialorderT userspecialordert = getSpecialOrdertInfo(reqConfirmAboardDTO.getOid());

		// 确认上车响应Bean
		ResConfirmAboardDTO resConfirmAboardDTO = new ResConfirmAboardDTO();

		// 订单不存在
		if (userspecialordert == null) {

			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(ConfirmAboardCodeMsg.ORDER_NOT_EXIST);// 订单不存在
			response.setPromptMessage(ConfirmAboardCodeMsg.ORDER_NOT_EXIST);// 订单不存在
			return false;
		}

		// 订单状态不是待上车
		if (userspecialordert.getOrderStatus() != 2) {

			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(ConfirmAboardCodeMsg.ORDER_STATUS_ERROR);// 该订单不是待上车状态
			response.setPromptMessage(ConfirmAboardCodeMsg.ORDER_STATUS_ERROR);// 该订单不是待上车状态
			return false;
		}

		// 订单状态
		userspecialordert.setOrderStatus(3);
		// 上车纬度
		userspecialordert.setAboardLongitude(reqConfirmAboardDTO.getAboardLongitude());
		// 上车经度
		userspecialordert.setAboardLatitude(reqConfirmAboardDTO.getAboardLatitude());
		// 上车时间
		userspecialordert.setAboardTime(new Date());
		
		// 设置订单状态
		resConfirmAboardDTO.setOrderStatus(3);

		response.setCode(HttpStatus.SC_OK);
		response.setResponseBody(resConfirmAboardDTO);

		return true;
	}

}
